package com.cat.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cat.admin.service.SysRoleService;
import com.cat.common.entity.SysRole;
import com.cat.common.entity.UserInfo;
import com.cat.common.util.SecurityUtils;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author dd-cat
 * @version 1.0
 * @date 2020-10-12 17:06
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/role")
@Api(value = "role", tags = "角色模块")
public class SysRoleController {
    private final SysRoleService roleService;


    @GetMapping("list")
    public List<SysRole> list(SysRole role) {
        return roleService.list(Wrappers.query(role));
    }

    /**
     * 通过ID查询
     *
     * @param id ID
     */
    @GetMapping("/{id}")
    public SysRole getById(@PathVariable Integer id) {
        return roleService.getById(id);
    }

    /**
     * 添加
     *
     * @param role 实体
     */
    @PostMapping
    public boolean save(@Valid @RequestBody SysRole role) {
        return roleService.save(role);
    }

    /**
     * 删除
     *
     * @param ids ids
     */
    @DeleteMapping("/{ids}")
    public boolean removeByIds(@PathVariable List<Long> ids) {
        return roleService.removeByIds(ids);
    }

    /**
     * 编辑
     *
     * @param sysRole 实体
     */
    @PutMapping
    public boolean update(@Valid @RequestBody SysRole sysRole) {
        UserInfo user = SecurityUtils.getUser();
        sysRole.setUpdateTime(LocalDateTime.now());
        sysRole.setUpdateBy(user.getUsername());
        return roleService.updateById(sysRole);
    }
}
